package com.wenjie.springbootbugdetection.web;

import com.wenjie.springbootbugdetection.model.*;
import com.wenjie.springbootbugdetection.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Yang wenjie
 * @date 2021/7/5 9:10
 * Nice to Meet you!
 */
@Controller
public class BugController {
    @Autowired
    private BugService bugService;
    @Autowired
    private DevelopmentService developmentService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private StatusService statusService;
    @Autowired
    private ModelService modelService;

    /**
     * Admin管理员能够修改Bug的所有属性
     * @return Bug记录界面
     */
    @RequestMapping(value = "/findAllBugs_Admin")
    public String findAllBugs_Admin(Model model){
        List<Bug> bugList_submitter_admin = bugService.selectAllBugs();

        Map<String, Object> statusMap = selectAllStatus();
        model.addAttribute("statusMap",statusMap);
        Map<String, Object> typeMap = selectAllTypes();
        model.addAttribute("typeMap",typeMap);

        model.addAttribute("bugList_submitter_admin",bugList_submitter_admin);

        return "/main/resources/templates/Bug.html";
    }

    /**
     * bug提交者  submitter
     * @return 跳转提交者看到的Bug记录界面
     */
    @RequestMapping(value = "/findAllBugs_submitter")
    public String findAllBugs_submitter(Model model){
        List<Bug> bugList_submitter = bugService.selectAllBugs();

        Map<String, Object> statusMap = selectAllStatus();
        model.addAttribute("statusMap",statusMap);
        Map<String, Object> typeMap = selectAllTypes();
        model.addAttribute("typeMap",typeMap);
        Map<String, Object> modelMap = selectAllModels();
        model.addAttribute("modelMap",modelMap);

        model.addAttribute("bugList_submitter",bugList_submitter);
        return "Bug_record_submitter";
    }
    @GetMapping("/add")
    public String bugAdd(Model model) {
        model.addAttribute("bug", new Bug());
        Map<String, Object> typeMap = selectAllTypes();
        model.addAttribute("typeMap",typeMap);
        Map<String, Object> modelMap = selectAllModels();
        model.addAttribute("modelMap",modelMap);

        return "bug_add_form";
    }

    @PostMapping("/doAdd")
    public String greetingSubmit(@ModelAttribute Bug bug) {
        int i  = bugService.insertABug(bug);
        if (i > 0){
            return "redirect:/findAllBugs_submitter";
        }else {
            return "wrong";
        }
    }

    @RequestMapping("/addModel_type")
    public String addModel_type(Model model) {
        model.addAttribute("Model_type", new Model_type());
        return "model_add_form";
    }

    @RequestMapping("/doAddModel_type")
    public String doAddModel_type(@ModelAttribute Model_type model_type) {
        int i  = modelService.insertAModelType(model_type);
        if (i > 0){
            return "redirect:/add";
        }else {
            return "wrong";
        }
    }


    /**
     * 测试经理  test_manager
     * @return 跳转测试经理能够看到的测试经理Bug记录界面（可以进行Bug的状态修改和删除）
     */
    @RequestMapping(value = "/findAllBugs_test_manager")
    public String findAllBugs_test_manager(Model model){
        List<Bug> bugList_test_manager = bugService.selectAllBugs();
        model.addAttribute("bugList_test_manager",bugList_test_manager);

        Map<String, Object> statusMap = selectAllStatus();
        model.addAttribute("statusMap",statusMap);
        Map<String, Object> typeMap = selectAllTypes();
        model.addAttribute("typeMap",typeMap);
        Map<String, Object> modelMap = selectAllModels();
        model.addAttribute("modelMap",modelMap);

        return "Bug_record_test_manager";
    }

    /**
     * @param bugId 传入bugId，能够删除相应的Bug记录
     * @return 重定向到测试经理更新后的的界面
     */
    @RequestMapping(value = "/delect")
    public String deleteABug(Model model, @RequestParam("bugId") String bugId){
        int i = bugService.delectABug(Integer.valueOf(bugId));
        if (i != 1){
            return "wrong";
        }else{
            return "redirect:/findAllBugs_test_manager";
        }
    }

    @RequestMapping(value = "/showABug")
    public String updateABug(Model model, @RequestParam("bugId") String bugId){
        Bug bug = bugService.queryBug(Integer.valueOf(bugId));
        model.addAttribute("bug",bug);

        Map<String, Object> statusMap = selectAllStatus();
        model.addAttribute("statusMap",statusMap);
        Map<String, Object> typeMap = selectAllTypes();
        model.addAttribute("typeMap",typeMap);
        if (bug == null){
            return "wrong";
        }else{
            return "bug_update_form";
        }
    }

    @RequestMapping(value = "/update")
    public String updateABug(@ModelAttribute Bug bug,HttpServletRequest request){
        int i = bugService.updateABug(bug);
        Integer test_managerId = (Integer) request.getSession().getAttribute("id");
        int j = bugService.checkedABug(bug.getBugId(),test_managerId);
        if (i != 1 | j!= 1){
            return "wrong";
        }else{
            return "redirect:/findAllBugs_test_manager";
        }
    }
    /**
     *
     * @param bugId 传入bugId，更改Bug的状态从审核中到解决中
     * @return 重定向到测试经理更新后的的界面
     */
    @RequestMapping(value = "/checked")
    public String checkedABug(@RequestParam("bugId") String bugId,@RequestParam("testManagerId") String testManagerId){
        int i = bugService.checkedABug(Integer.valueOf(bugId),Integer.valueOf(testManagerId));
        if (i != 1){
            return "wrong";
        }else{
            return "redirect:/showABug";
        }
    }

    @RequestMapping(value = "/checkedFalse")
    public String checkedFalse(@RequestParam("bugId") String bugId,@RequestParam("testManagerId") String testManagerId){
        int i = bugService.checkedABugFalse(Integer.valueOf(bugId),Integer.valueOf(testManagerId));
        if (i != 1){
            return "wrong";
        }else{
            return "redirect:/findAllBugs_test_manager";
        }
    }


    /**
     * 开发工程师  Development
     * @return 跳转至开发工程师Bug记录界面（可以进行Bug状态修改）
     */
    @RequestMapping(value = "/findAllBugs_development")
    public String findAllBugs_development(Model model,HttpServletRequest request){
        Integer ID = (Integer) request.getSession().getAttribute("id");
        Development development = developmentService.queryADevelopment(ID);

        List<Bug> bugList_development = developmentService.selectAllBugs(3,4,development.getModelId());

        Map<String, Object> statusMap = selectAllStatus();
        model.addAttribute("statusMap",statusMap);
        Map<String, Object> typeMap = selectAllTypes();
        model.addAttribute("typeMap",typeMap);
        Map<String, Object> modelMap = selectAllModels();
        model.addAttribute("modelMap",modelMap);

        model.addAttribute("bugList_development",bugList_development);
        return "Bug_record_development";
    }
    /**
     * @param bugId 传入BugId
     * @return 重定向到 /bug/findAllBugs_development
     */
    @RequestMapping(value = "/solved")
    public String solvedABug(@RequestParam("bugId") String bugId,@RequestParam("developmentId") String developmentId){
        int i = bugService.solvedABug(Integer.valueOf(bugId),Integer.valueOf(developmentId));
        return "redirect:/findAllBugs_development";
    }
/*****************************以下是工具方法***********************************/
    private Map<String, Object> selectAllTypes() {
        Map<String, Object> types = new HashMap<>();
        List<Bug_type> typeList = typeService.selectAllTypes();
        for (Bug_type atype : typeList){
            types.put(atype.getTypeName(),atype.getTypeId());
        }
        return types;
    }

    private Map<String, Object> selectAllStatus() {
        Map<String, Object> statusMap = new HashMap<>();
        List<Bug_status> statusList = statusService.selectAllStatus();
        for (Bug_status status : statusList){
            statusMap.put(status.getStatusName(),status.getStatusId());
        }
        return statusMap;
    }
    private Map<String, Object> selectAllModels() {
        Map<String, Object> modelsMap = new HashMap<>();
        List<Model_type> modelTypeList = modelService.selectAllModels();
        for (Model_type model_type : modelTypeList){
            modelsMap.put(model_type.getModelPackage(),model_type.getModelId());
        }
        return modelsMap;
    }
}
